﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.TranslateApprove.Model;
using Yipusoft.TranslateApprove.BLL;
using System.Web.UI.HtmlControls;

public partial class Police_TranslateApprove_AppalyList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            bindGV(0);
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        bindGV(PageIndex);
    }

    void bindGV(int pageIndex)
    {
        string where = " UserID='" + Page.User.Identity.Name + "' ";
        List<ApplyInfo> list = Apply.GetList(ListPageContral1.PageSize, pageIndex, where);
        ListPageContral1.ItemCount = Apply.GetCount(where);
        UserGrid.DataSource = list;
        UserGrid.DataBind();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //查看

            HyperLink ApplyTitle = e.Row.FindControl("ApplyTitle") as HyperLink;
            ApplyTitle.NavigateUrl = "AppalyDisplay.aspx?Fid=" + UserGrid.DataKeys[e.Row.RowIndex].Value;
            ApplyTitle.Text = DataBinder.Eval(e.Row.DataItem, "Title").ToString();
            //提交角色列表
            DropDownList ddlRoles = e.Row.FindControl("ddlRoles") as DropDownList;
            ddlRoles.DataSource = Process.GetJumpList(1, 0);
            ddlRoles.DataBind();
            LinkButton lkbRefer = e.Row.FindControl("lkbRefer") as LinkButton;
            //状态
            lkbRefer.CommandArgument = e.Row.RowIndex.ToString();
            Label state = e.Row.FindControl("state") as Label;
            //未上报
            HtmlGenericControl NoRefer = e.Row.FindControl("NoRefer") as HtmlGenericControl;
            //已上报
            HtmlGenericControl Refer = e.Row.FindControl("Refer") as HtmlGenericControl;
            //是否审批
            string where = "Fid='" + UserGrid.DataKeys[e.Row.RowIndex].Value + "'";
            List<SetupLogInfo> list = SetupLog.GetList(where);
            if (list.Count > 0)
            {
                NoRefer.Visible = false;
                //state.Text = "已上报";
                //var list2 =from pp in list where pp.PID;\
                Label ApproveState = e.Row.FindControl("ApproveState") as Label;
                ApproveState.Text = SetupLog.GetCurrentState(new Guid(UserGrid.DataKeys[e.Row.RowIndex].Value.ToString()));
                if (ApproveState.Text == "审批通过")
                {
                    ApproveState.ForeColor = System.Drawing.Color.Red;
       
                }
            }
            else//没有在setuplogs表里
            {
                //state.Text = "未上报";
                Refer.Visible = false;
            }


        }
    }

    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            if (Apply.Delete(new Guid(e.CommandArgument.ToString())))
            {
                ListPageContral1.SetFirstPage();
                bindGV(0);
                Prompt.Text = "提示：删除一条记录！";
            }
        }
        if (e.CommandName == "Refer")
        {
            int rowIndex = Convert.ToInt32(e.CommandArgument);
            DropDownList ddlRoles = UserGrid.Rows[rowIndex].FindControl("ddlRoles") as DropDownList;


            if (ddlRoles.SelectedValue == "")
            {
                Prompt.Text = "提示：请选择要上报的角色!";
            }
            else
            {
                try
                {
                    int PID = Convert.ToInt32(ddlRoles.SelectedValue.ToString());
                    Yipusoft.TranslateApprove.Model.ProcessInfo process = Process.GetModel(PID);
                    SetupLogInfo setup = new SetupLogInfo();
                    setup.FID = new Guid(UserGrid.DataKeys[rowIndex].Value.ToString());
                    setup.KID = 1;
                    setup.PID = Process.GetModel(1, 0).PID;
                    setup.Remark = "";
                    setup.RTime = DateTime.Now;
                    setup.State = 1;
                    setup.UserID = User.Identity.Name;
                    setup.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                    SetupLog.Add(setup);
                    string where = " Kid=1 and orderid>0 and orderid<" + process.OrderID + "  ";
                    List<Yipusoft.TranslateApprove.Model.ProcessInfo> list = Process.GetList(where);
                    foreach (Yipusoft.TranslateApprove.Model.ProcessInfo info in list)
                    {
                        SetupLogInfo setup1 = new SetupLogInfo();
                        setup1.FID = new Guid(UserGrid.DataKeys[rowIndex].Value.ToString());
                        setup1.KID = 1;
                        setup1.PID = info.PID;
                        setup1.Remark = "";
                        setup1.RTime = DateTime.Now;
                        setup1.State = 4;
                        setup1.UserID = User.Identity.Name;
                        setup1.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
                        SetupLog.Add(setup1);
                    }
                    Prompt.Text = "提示：上报成功！";
                    ListPageContral1.SetFirstPage();
                    bindGV(0);
                }
                catch { }
            }
        }

    }
}
